package com.raysdata.smartcity.web.sys;

import com.raysdata.smartcity.HelperClazz;
import com.raysdata.smartcity.Key;
import com.raysdata.smartcity.Md5;
import com.raysdata.smartcity.dao.sys.UserDao;
import com.raysdata.smartcity.dto.User;
import com.raysdata.smartcity.web.BaseController;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by yl on 2016/10/14.
 */
@Controller
@RequestMapping(value = "/account")
public class AccountController extends BaseController {
    @Autowired
    public UserDao userDao;

    //首页跳转
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        return "login";
    }

    // 系统登出
    @RequestMapping("/loginOut")
    public String loginOut(HttpServletRequest request) {
        HttpSession session = request.getSession();
        /*User user = (User) session.getAttribute("LoginUser");
        logDao.saveLog("退出系统",Key.LOG_EXIT,user,user.getLogin_name(), Key.LOG_SUCCESS);*/
        session.invalidate();
        return "redirect:/account/login";
    }

    @RequestMapping("/toMain")
    public String toMain(HttpServletRequest request, HttpServletResponse response){
        return "smartcity/main";
    }

    @RequestMapping("/toMainInit")
    public String toMainInit(HttpServletRequest request, HttpServletResponse response){
        return "smartcity/main/mainInit";
    }

    // 系统登录
    @RequestMapping("/login")
    public String login(Model model, HttpServletRequest request, HttpServletResponse response) {
        return "login";
    }
    @RequestMapping("/loginSub")
    public void loginSub(Model model, HttpServletRequest request, HttpServletResponse response){
        String loginName = request.getParameter("loginName");
        String loginPwd = request.getParameter("loginPwd");
        // XXX MD5加密
        if (null != loginPwd) {
            loginPwd = Md5.MD5(loginPwd);
            System.out.println(loginPwd);
        }
        String code = request.getParameter("j_code");
        String code_true = (String) request.getSession().getAttribute("validateCode");
        if(!code.equals(code_true)){
            HelperClazz.renderJson(response, "{\"success\":false,\"msg\":\"验证码输入错误！\"}");
            return;
        }
        User user=new User();
        try {
            HttpSession session = request.getSession();
            // 查询登录
            user = userDao.login(loginName, loginPwd);
            if (null == user) {
                HelperClazz.renderJson(response, "{\"success\":false,\"msg\":\"用户名或密码错误！\"}");
                return;
            } else {
                session.setAttribute("LoginUser", user);
                logDao.saveLog(request,"登录系统",Key.LOG_LOGIN,user,loginName, Key.LOG_SUCCESS);
                HelperClazz.renderJson(response, "{\"success\":true,\"msg\":\"登录成功！\"}");
                return;
            }
        } catch (Exception e) {
            logDao.saveLog(request,"登录系统",Key.LOG_LOGIN,user,loginName, Key.LOG_ERROR);
            HelperClazz.renderJson(response, "{\"success\":false,\"msg\":\"登录异常！\"}");
            e.printStackTrace();
            return;
        }
    }
    @RequestMapping("/casLogin")
    public String casLogin(Model model, HttpServletRequest request, HttpServletResponse response){
        AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
        String loginName=principal.getName();
        User user=userDao.getUserByLoginName(principal.getName());
        if(user==null){
            return "error/noPermissions";
        }
        HttpSession session = request.getSession();
        session.setAttribute("LoginUser", user);
        logDao.saveLog(request,"登录系统",Key.LOG_LOGIN,user,loginName, Key.LOG_SUCCESS);
//        Map attributes = principal.getAttributes();
//        String username=attributes.get("login_name").toString();
        return "smartcity/main";
    }
}
